package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bean.LeaveDetailsBean;
import com.bean.LeaveInfoBean;
import com.exception.LMSException;

/**
 * The Class LeaveDetailsDAO.
 * 
 * @author : gbs02399 Created on : Jul 6, 2012 Project : LMS File Name :
 *         LeaveDetailsDAO.java
 */
public class LeaveDetailsDAO {

	/**
	 * Insert leave.
	 * 
	 * @param leaveDetailsBean
	 *            the employee bean
	 * @throws LMSException
	 *             the lMS exception
	 */
	public static void insertLeave(final LeaveDetailsBean leaveDetailsBean,LeaveInfoBean infoBean)
			throws LMSException {
		Connection con = null;
		final Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection(
					"jdbc:oracle:thin:@127.0.0.1:1521:xe", "system",
					"pass");
			preparedStatement = con
					.prepareStatement("insert into LEAVE_DETAILS values (LEAVE_DETAILS_SEQ.nextval,?,?,?,?,?,?)");

			preparedStatement.setLong(1, leaveDetailsBean.getLeaveId());
			preparedStatement.setDate(2, new java.sql.Date(leaveDetailsBean.getStartDate().getTime()));
			preparedStatement.setDate(3, new java.sql.Date(leaveDetailsBean.getEndDate().getTime()));
			preparedStatement.setString(4, leaveDetailsBean.getReason());
			preparedStatement.setLong(5, leaveDetailsBean.getNoOfDays());
			preparedStatement.setString(6, leaveDetailsBean.getState());
			preparedStatement.executeUpdate();
			
			preparedStatement = con.prepareStatement("update leave_info set leave_balance = ? where leave_id = ?");
			preparedStatement.setLong(1, infoBean.getLeaveBalance());
			preparedStatement.setLong(2, infoBean.getLeaveId());
			preparedStatement.executeUpdate();
			
		} catch (final SQLException e) {
			e.printStackTrace();
			throw new LMSException(e.getMessage());
		} catch (final ClassNotFoundException e) {
			e.printStackTrace();
			throw new LMSException(e.getMessage());
		} finally {
			ConnectionCloser.closeConnection(null, connection,
					preparedStatement);
		}
	}
	
	public static List<LeaveDetailsBean> listOfLeaves(Long leaveId) throws LMSException {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		final List<LeaveDetailsBean> leaveList = new ArrayList<LeaveDetailsBean>();
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@127.0.0.1:1521:xe", "system",
					"pass");
			preparedStatement = connection
					.prepareStatement("select ld.LEAVE_DET_ID,ld.LEAVE_ID,ld.START_DATE,ld.END_DATE,ld.REASON,ld.TOTAL_NO_DAYS,ld.STATE from LEAVE_DETAILS ld where ld.leave_id in(?)");
			preparedStatement.setLong(1, leaveId);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				final LeaveDetailsBean leaveDetailsBean = new LeaveDetailsBean();
				leaveDetailsBean.setLeaveDetailsid(Long.valueOf(resultSet.getString("LEAVE_DET_ID")));
				leaveDetailsBean.setLeaveId(Long.valueOf(resultSet.getString("LEAVE_ID")));
				leaveDetailsBean.setStartDate(resultSet.getDate("START_DATE"));
				leaveDetailsBean.setEndDate(resultSet.getDate("END_DATE"));
				leaveDetailsBean.setReason(resultSet.getString("REASON"));
				leaveDetailsBean.setNoOfDays(Long.valueOf(resultSet.getString("TOTAL_NO_DAYS")));
				leaveDetailsBean.setState(resultSet.getString("STATE"));
				System.out.println(leaveDetailsBean.toString());
				leaveList.add(leaveDetailsBean);
			}
		} catch (final SQLException e) {
			e.printStackTrace();
			throw new LMSException(e.getMessage());
		} catch (final ClassNotFoundException e) {
			e.printStackTrace();
			throw new LMSException(e.getMessage());
		} finally {
			ConnectionCloser.closeConnection(resultSet, connection,
					preparedStatement);
		}
		return leaveList;
	}
}
